package com.jketing.service.ams.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jketing.entity.ams.base.Employee;
import com.jketing.entity.ams.base.Organ;
import com.jketing.repository.BaseRepository;
import com.jketing.repository.ams.base.EmployeeRepository;
import com.jketing.service.BaseService;
import com.jketing.service.PredicateBuilder;


/**
 *  EmployeeService	功能服务类
 * 	功能：基类实现单表CRUD及分页条件查询，继承扩展实现多表查询及多表操作等（若有需要）；
 * 		可注入其它实体的仓库类或使用jdbc实现Dao调用本地Sql进行多表查询
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2017-12-01
 * @since   Spring-data 1.11.4
 */
@Service
public class EmployeeService extends BaseService<Employee,String> {
	
	/**
	 * repository String按IOC容器管理的Bean类型自动注入模块仓库操作实例 .
	 */
	@Autowired
	private EmployeeRepository repository;
	
	/**
	 * setBaseRepository 按IOC容器管理的Bean名称自动注入服务基类的仓库操作实例
	 * 	 
	 * @param BaseRepository<Employee,String> repository
	 * 
	 * @return void
	 */
	@Resource(name="employeeRepository")
	public void setBaseRepository(BaseRepository<Employee,String> repository) {
		super.setBaseRepository(repository);
		PredicateBuilder<Employee> pb = new PredicateBuilder<Employee>();
		setPredicateBuilder(pb);
	}
	
	private List<String> getOrganIds(String id){
		Employee entity = repository.findOne(id);
		List<String> result = new ArrayList<String>();
		if(entity.getOrgans().size() > 0){
			for(Organ item:entity.getOrgans()){
				result.add(item.getId());
			}	
		}
		return result;
	}
	
	public List<Organ> getOrgans(String id){
		Employee entity = repository.getOne(id);
		List<Organ> result = new ArrayList<Organ>();
		if(entity.getOrgans()!=null){
			for(Organ item:entity.getOrgans()){
				result.add(item);
			}	
		}
		return result;
	}
	
	/**
	 * 新增时调用的save方法
	 */
	public Employee save(Employee entity,String ids) {
		if(StringUtils.isNotBlank(ids)) {
			String[] array = ids.split(",");
			for(String id:array) {
				Organ o = new Organ();
				o.setId(id);
				entity.add(o);
			}
		}
		entity.setId(getTimeId());
		return repository.save(entity);
	}
	
	/**
	 * 更新时调用的update方法
	 */
	public Employee update(Employee entity,String ids) {
		if(StringUtils.isNotBlank(ids)) {
			List<String> newIds = Arrays.asList(ids.split(","));
			List<String> originalIds = getOrganIds(entity.getId());
			// 从originalIds去除在newIds中出现的元素，剩下的就是要删除关系的元素
			//originalIds.removeAll(newIds);差集
			Employee object = repository.findOne(entity.getId());			
			//删除原来的所在部门关系
			for(String id:originalIds) {
				Organ o = new Organ();
				o.setId(id);
				object.remove(o);
			}
			repository.flush();
	        // 从newIds去除在originalIds中出现的元素，剩下的就是要删除关系的元素
			//newIds.removeAll(originalIds);
			//增加新提交的所在部门关系
			for(String id:newIds) {
				Organ o = new Organ();
				o.setId(id);
				entity.add(o);
			}
		}
		return repository.saveAndFlush(entity);
	}
}
